//
//  BaseTableViewController.m
//  TheRicher
//
//  Created by 李晓东 on 2018/1/16.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//



#import "BaseTableViewController.h"
#import "NetMessageTools.h"

static NSInteger RefreshViewHeight = 60;

typedef NS_ENUM(NSInteger, CurrentState) {
    idle = 0,
    pulling,
    refreshing,
    loading,
    loaded
};

@interface BaseTableViewController ()
@property (nonatomic, strong) NSObject *observer;
@property (nonatomic, assign) NSInteger currentState;
@property (nonatomic, strong) LoadMoreFromDatabaseBlock block;
@property (nonatomic, strong) CallNetBlock callNetBlock;
@property (nonatomic, strong) XDRefreshingView *refreshingView;
@end

@implementation BaseTableViewController

- (void)loadView{
    [super loadView];
    self.killingNumberTools = [KillNumberTools share];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.automaticallyAdjustsScrollViewInsets
    _activityIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [_activityIndicatorView setColor:[UIColor redColor]];
    [self.tableView setTableFooterView:_activityIndicatorView];
    
    _currentState = idle;
    __weak typeof(self) weakSelf = self;
    _block = ^(void){
        [weakSelf.databaseService queryTable:ShuangSeQiuTableName withCondition:[weakSelf.dataArray.lastObject issueno] ofConditionName:@"issueno" andJudgeFlag:@"<" andOrderBy:@"issueno" andSortBy:1 andCountOf:RowCount];
    };
    self.netService.delegate = self;
    self.killingNumberTools.delegate = self;
    _callNetBlock = ^ (void){
        [weakSelf.netService loadDataFromIssueno:nil andID:11 andCallBlock:nil];
    };
    _refreshingView = [[XDRefreshingView alloc]initWithFrame:CGRectMake(0, -64, self.view.frame.size.width, 64) andBlock:_callNetBlock];
    
    [self.tableView setRefreshingView:_refreshingView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

// 加载数据库
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.tableView scrollToEnd:_block];
    
}

// 只适用于下拉刷新操作
- (void)resultInNet:(id)result{
    NSArray *resultArray = [NetMessageTools changeIntoShuangSeQiu:result];
    // 这个方法的实现,在每个子 Controller 中都不相同.主要用来对 生成killingNumber
    [self typeMethodInKillNumberTools:resultArray];
    ShuangSeQiu *lastestObj = (ShuangSeQiu *)resultArray.firstObject;
    
    NSInteger difCount = [lastestObj.issueno integerValue] - [[(ShuangSeQiu *)_dataArray.firstObject issueno] integerValue];
    if (_needToRefreshCount == 0) {
        _needToRefreshCount = difCount;
    }
    if (resultArray.count == 1 && (difCount == 1 || difCount == -1)) {
        [_dataArray insertObject:lastestObj atIndex:difCount > 0 ? 0 : 1];
        [self refreshChildControllerView:_needToRefreshCount];
        [[DatabaseService share]insertTable:nil withObject:resultArray];
        [_refreshingView stopAnimating];
    } else if (resultArray.count > 1 || labs(difCount) > 1) {
        if (resultArray.count > 1) {
            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, resultArray.count)];
            [_dataArray insertObjects:resultArray atIndexes:indexSet];
            [self refreshChildControllerView:_needToRefreshCount];
            [[DatabaseService share]insertTable:nil withObject:resultArray];
            [_refreshingView stopAnimating];
        } else {
            [_dataArray insertObject:lastestObj atIndex:0];
            [self.netService loadDataFromIssueno:lastestObj.issueno andNumOf:difCount - 1 andID:11 andCallBlock:nil];
            [[DatabaseService share]insertTable:nil withObject:@[lastestObj]];
        }
    } else if (difCount == 0) {
        [_refreshingView stopAnimating];
    }
    
}

@end
